package com.atguigu.flink.chapter05.Transform;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/8/9 10:30
 */
public class Flink07_Connect {
    public static void main(String[] args) throws Exception {
        // 0.执行环境
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        // 1.读取数据
        //  sensor_1,123123213,10
        DataStreamSource<Integer> numDS = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8);
        DataStreamSource<String> strDS = env.fromElements("a", "b", "c");
        DataStreamSource<String> strDS2 = env.fromElements("a", "b", "c");

        // TODO
        ConnectedStreams<Integer, String> connectDS = numDS.connect(strDS);


        SingleOutputStreamOperator<String> resultDS = connectDS.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return "1：我接受了。.."+value.toString();
            }

            @Override
            public String map2(String value) throws Exception {
                return "2: 我放弃挣扎了。.."+value;
            }
        });


        resultDS.print();


        env.execute();
    }
}

/*
    connect: 连接两条流
        1. 一次只能连接两条流
        2. 两条流的数据类型可以不一样
 */
